<?php
	
	include_once("Login.php");

	
	
	$conn = new mysqli($host,$user,$passwd, $database)
	
	or die("SQL connect error: ".mysqli_connect_error());
	
	

	$body = file_get_contents('php://input');
	$xml = simplexml_load_string($body);
	$userId = $xml->userId;	
	
	
	
	$query = "SELECT m.Id as id, m.ToUserId as toid, m.FromUserId as fromid, t.Username as toname, f.Username as fromname, m.Message as message, m.DateSent as date, m.ToKeep as tokeep, m.FromKeep as fromkeep FROM Message m JOIN User t ON m.ToUserId = t.Id JOIN User f ON f.Id = m.FromUserId WHERE m.ToUserId='$userId' OR m.FromUserId='$userId'";
	//"SELECT * FROM Message JOIN User t JOIN User f ON Message.ToUserId = User.Id WHERE Message.ToUserId='$userId' OR Message.FromUserId='$userId'";
	
		
	
	
	
	$result = mysqli_query($conn, $query);

	
	
	/*?>
	
	<table border=1>
	
		<tr>
	
			<th>
				id
			</th>
			<th>
	
				toId
	
			</th>
	
			<th>
	
				toName
	
			</th>
	
			<th>
	
				fromId
	
			</th>
	
			<th>
	
				fromName
	
			</th>
	
			<th>
	
				message
	
			</th>
	
			<th>
				dateSent
			</th>
			<th>
				toKeep
			</th>
			<th>
				fromKeep
			</th>
		</tr>
	
	<?php
*/	
	echo "<node>";
	while($row = mysqli_fetch_array($result))
	
	{
		
		
	echo "<newmessage>";
			echo "<id>";
			
				echo $row['id'];		
			echo "</id>";
		
			echo "<toid>
";		
				echo $row['toid'];			
			echo "</toid>"
;			
			echo "<toname>"
;			
				echo $row['toname'];			
			echo "</toname>
";			
			echo "<fromid>"
;			
				echo $row['fromid'];

		
			echo "</fromid>
";			
			echo "<fromname>"
;			
				echo $row['fromname'];

			
			echo "</fromname>
";	
			echo "<message>
";			
				echo $row['message'];		
			echo "</message>
";	
			echo "<date>
";		
				echo $row['date'];			
			echo "</date>"
;	
			echo "<tokeep>"
;			
				echo $row['tokeep'];			
			echo "</tokeep>"
;	
			echo "<fromkeep>"
;			
				echo $row['fromkeep'];			
			echo "</fromkeep>";
			echo "</newmessage>";
			
			
	}
	
	echo "</node>";	
		mysqli_close($conn);

?>


